<?php
/**
 * Create by wenyi
 * E-mail: wesley_prc@163.com
 * Description: vue-admin-element-api
 * Date: 2024/2/21 17:17
 * Update: 2024/2/21 17:17
 */

namespace app\api\controller;

use app\BaseController;
use think\facade\Db;

class System extends BaseController
{
    public function index()
    {
        $data = ['version'=>'v1.3.9','server'=>'DSIS'];
        return returnResult(config("config.code.success"),'测试',$data);
    }
    public function me()
    {
        $data = [
            'id'=>5,
            'jobNo'=>'001',
            'avatar'=>null,
            'name'=>"管*员",
            'gender'=>0,
            'account'=>'001',
            'hisCode'=>'001',
            'type'=>0,
            'pinyin'=>'GUANLIYUAN',
            'department'=>[
                'id'=>11,
                'departmentNo'=>'8901',
                'departmentName'=>'日间手术中心',
            ],
            'jobPosition'=>null,
            'hospitalArea'=>null,
            'role'=>[
               [ 'id'=>12,
                   'slug'=>'administrator',
                   'name'=>'管理员'
               ],
                [ 'id'=>13,
                    'slug'=>'central_doctor',
                    'name'=>'中心医生'
                ],
                [ 'id'=>14,
                    'slug'=>'central_nurse',
                    'name'=>'中心护士'
                ],
                'status'=>true
            ]
        ];
        return returnResult(config("config.code.success"),'测试',$data);
    }
    public function menus()
    {
//        $data = [[
//            'id'=>2,
//            'parentId'=>0,
//            'path'=>"/dsProcess",
//            "name"=>"dsProcess",
//            "component"=>"dsProcess/overview/index",
//            "redirect"=>"/dsProcess/overview",
//            "meta"=>[
//                "title"=>"患者全流程",
//                "isLink"=>"",
//                "isHide"=>false,
//                "isKeepAlive"=>false,
//                "isAffix"=>false,
//                "isIframe"=>false,
//                "icon"=>"ele-SetUp",
//                "children"=>[
//                    "id"=>3,
//                    "parentId"=>2,
//                    "path"=>"/dsProcess/overview",
//                    "name"=>"dsProcess-overview",
//                    "component"=>"dsProcess/overview/index",
//                    "redirect"=>null,
//                    "meta"=>[
//                        "title"=>"患者全览",
//                        "isLink"=>"",
//                        "isHide"=>false,
//                        "isKeepAlive"=>false,
//                        "isAffix"=>false,
//                        "isIframe"=>false,
//                        "icon"=>"ele-Postcard",
//                    ],
//                    "children"=>[
//                        "id"=>5,
//                        "parentId"=>3,
//                        "path"=>"/dsProcess/overview/patientDetail",
//                        "name"=>"dsProcess-overview-patientDetail",
//                        "component"=>"dsProcess/overview/patientDetail",
//                        "redirect"=>null,
//                        "meta"=>[
//                            "title"=>"患者详情",
//                            "isLink"=>"",
//                            "isHide"=>true,
//                            "isKeepAlive"=>false,
//                            "isAffix"=>false,
//                            "isIframe"=>false,
//                            "icon"=>null,
//                        ],
//                    ]
//                ]
//            ]
//        ]];
        $q = Db::table('menu')->select();
//        $menuApiList = $this->getMenuApiIds($q);
        $ss = $this->getTreeData($q->toArray());
//        var_dump($ss);
        return returnResult(config("config.code.success"),'测试',$ss);
    }
    /**
     * 获取树状菜单列表
     * @param $menuList
     * @param int $parentId
     * @return array
     */
    protected function getTreeData($menuList, int $parentId = 0)
    {
        try {
            $data = [];
            foreach ($menuList as $key => $item) {
                if ($item['parentId'] == $parentId) {
                    $children = $this->getTreeData($menuList, (int)$item['id']);
//                    var_dump(!empty($children))
                    if($children){
//                        var_dump($item);
                        $item['children'] = $children;
                    }
                    $item['meta'] = [
                        'icon'=>$item['icon'],
                        'isAffix'=>$item['isAffix']=='1'? false : ($item['isAffix'] == '0' ? false : true),
                        'isHide'=>$item['isHide']=='1'? false : ($item['isHide'] == '0' ? false : true),
                        'isIframe'=>$item['isIframe']=='1'? false : ($item['isIframe'] == '0' ? false : true),
                        'isKeepAlive'=>$item['isKeepAlive']=='1'? false : ($item['isKeepAlive'] == '0' ? false : true),
                        'isLink'=>'',
                        'title'=>$item['title'],
                        ];
                    unset($item['icon']);
                    unset($item['isAffix']);
                    unset($item['isHide']);
                    unset($item['isIframe']);
                    unset($item['isKeepAlive']);
                    unset($item['isLink']);
                    unset($item['title']);
//                    !empty($children) && $item['children'] = $children;
                    $data[] = $item;
                    unset($menuList[$key]);
                }
            }
            return $data;
        }catch (\Exception $e){
            exception('MenuBus.getTreeData',$e);
        }
    }

    public function departments()
    {
        $q = Db::table('test_department')->select();
        return returnResult(config("config.code.success"),'测试',$q);
    }
    public function user()
    {
        $q = Db::table('users')->select();
//        var_dump($q);
        $data = [];
        $data['items'] = $q->toArray();
        $data['lastPage'] = 1;
        $data['total'] = 1;
        return returnResult(config("config.code.success"),'测试',$data);
    }
    public function hospital_area()
    {
        $q =['id'=>1,'name'=>'日间手术室'];
        return returnResult(config("config.code.success"),'测试',$q);
    }
}